package com.education.service.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.education.exception.OnlineEducationException;
import com.education.result.ResultCodeEnum;
import com.education.service.vod.service.VideoService;
import com.education.service.vod.utils.AliyunVodSDKUtils;
import com.education.service.vod.utils.VodProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.List;

/**
 * 上传视频的业务
 * Created by Lance on 2020/07/01 10:52
 */
@Slf4j
@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VodProperties properties;

    @Override
    public String uploadVideo(InputStream inputStream, String oFileName) {
        String title = oFileName.substring(0, oFileName.lastIndexOf("."));
        // 创建请求对象
        UploadStreamRequest request = new UploadStreamRequest(properties.getKeyId(), properties.getKeySecret(), title, oFileName, inputStream);
        // 指定转码模板组
        // request.setTemplateGroupId(properties.getTemplateGroupId());

        // 创建上传对象
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        String videoId = response.getVideoId();
        // 判断视频ID是否为空
        if (StringUtils.isEmpty(videoId)){
            // 为空做错误处理
            log.error("视频上传到阿里云服务器失败!" + response.getCode() + "======" + response.getMessage());
            throw new OnlineEducationException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR);
        }
        return videoId;
    }

    @Override
    public void removeVideo(String videoId) throws ClientException {
        // 初始化 client 对象
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                properties.getKeyId(),
                properties.getKeySecret());
        // 删除视频的请求请求对象
        DeleteVideoRequest request = new DeleteVideoRequest();
        // 支持多视频的ID,可以用逗号隔开
        request.setVideoIds(videoId);
        // 发送请求,删除视频
        client.getAcsResponse(request);
    }

    @Override
    public void removeVideoByIdList(List<String> videoIdList) throws ClientException {

        //初始化client对象
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                properties.getKeyId(),
                properties.getKeySecret());
        DeleteVideoRequest request = new DeleteVideoRequest();

        int size = videoIdList.size();
        StringBuffer idListStr = new StringBuffer();
        for (int i = 0; i < size; i++) {

            idListStr.append(videoIdList.get(i));
            // 判断视频数
            if(i == size -1 || i % 20 == 19){
                System.out.println("idListStr = " + idListStr.toString());
                //支持传入多个视频ID，多个用逗号分隔，最多20个
                request.setVideoIds(idListStr.toString());
                // 执行删除
                client.getAcsResponse(request);
                idListStr = new StringBuffer();
            }else if(i % 20 < 19){
                idListStr.append(",");
            }
        }
    }

    /**
     * 根据视频ID 获取阿里云播放凭证
     * @param videoSourceId 视频ID
     * @return
     */
    @Override
    public String getPlayAuth(String videoSourceId) throws ClientException {
        //初始化client对象
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                properties.getKeyId(),
                properties.getKeySecret());
        //创建请求对象
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest ();
        request.setVideoId(videoSourceId);

        //获取响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);
        // 获取播放凭证
        return response.getPlayAuth();
    }
}